package db.twitter;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;

import db.MySqlHandler;

public class TwitterDataLoader {

	public static void run(String filename, String tblname) {
		long size = 0;
		try {
			final String path = filename; // "/media/d/Datasets/twitter/" +
			final String insert = "insert into mytwitterdb." + tblname + " values (default, ?, ?, ?, ?, ?, ?)";
			final String connStr = "jdbc:mysql://localhost:3306/mytwitterdb";
			boolean isParamsEmpty = false;
			BufferedReader reader = new BufferedReader(new FileReader(path));
			String line;
			Connection con = MySqlHandler.getConnection(connStr, "user", "user123");
			PreparedStatement prep = con.prepareStatement(insert);
			reader.readLine();
			String part;
			while ((line = reader.readLine()) != null) {
				if (line.length() > 2) {
					part = line.substring(2);
				} else {
					part = "";
				}

				if (line.startsWith("U")) {
					prep.setString(1, part);
					isParamsEmpty = false;
				} else if (line.startsWith("T")) {
					prep.setString(2, part);
					isParamsEmpty = false;
				} else if (line.startsWith("W")) {
					prep.setString(3, part);
					String link = extractKeyPart(part, "http://", '.', '/');
					prep.setString(4, link);
					String friend = extractKeyPart(part, "@");
					prep.setString(5, friend);
					if (link == null && friend == null) {
						prep.clearParameters();
						isParamsEmpty = true;
						continue;
					}
					String keywords = extractKeywords(part);
					prep.setString(6, keywords);
					isParamsEmpty = false;
				}

				if (line.length() == 0 && !isParamsEmpty) {
					MySqlHandler.update(con, prep);
					prep.clearParameters();
					isParamsEmpty = true;
					size++;

					if ((size % 1000) == 0.0) {
						System.out.println(size / 1000);
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("done! " + size + " records has entered successfully from " + filename + " in table "
				+ tblname);
	}

	private static String extractKeywords(String part) {
		String[] s = part.split(" ");
		String keywords = null;
		for (String ss : s) {
			String r = extractKeyPart(ss, "#");
			if (r != null) {
				keywords = (keywords == null ? "" : keywords) + "," + r;
			}
		}
		return null;
	}

	private static String extractKeyPart(String part, String key) {
		return extractKeyPart(part, key, (char) Character.MIN_CODE_POINT);
	}

	private static String extractKeyPart(String part, String key, char... exception) {
		int idx = part.indexOf(key);
		if (idx >= 0) {
			int count = 0;
			int keyLen = key.length();
			for (; count < part.length() - (idx + keyLen); count++) {
				char c = part.charAt(idx + count + keyLen);
				if (!Character.isLetter(c) && !Character.isDigit(c)) {
					boolean isInExp = false;
					for (char cc : exception) {
						if (c == cc) {
							isInExp = true;
							break;
						}
					}
					if (!isInExp) {
						break;
					}
				}
			}
			return part.substring(idx, idx+count+keyLen);
		}
		return null;
	}
}
